// pages/cooperation/cooperation.js
var range;
var app = getApp()
var winScale = 750 / app.data.deviceInfo.windowWidth
var shihan = require('../../../common/fetch');
var config = require('../../../common/config');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    pagenum: 20,
    cooperationlist: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) { //get_cooperation_list
    var pages = getCurrentPages();
    if (pages.length >= 2) {
      var prevPage = pages[pages.length - 2];
      prevPage.setData({
        promotefresh: false
      })
    }
    this.changecooperationlist()
    range = 900 / winScale;
    var animation = wx.createAnimation({
      duration: 350,
      timingFunction: 'ease-in-out',
    });
    this.animation = animation;
  },
  changecooperationlist: function() {
    var that = this
    shihan.sendRequest(config.get_cooperation_list + that.data.page + '-' + that.data.pagenum + '.html', 'GET', {})
      .then(function(response) {
        if (response.data.code == 0) {
          if (response.data.data.sh_title) {
            wx.setNavigationBarTitle({
              title: response.data.data.sh_title
            })
          } else {
            wx.setNavigationBarTitle({
              title: "合作伙伴"
            })
          }
          that.setData({
            cooperation: response.data.data,
            cooperationlist: that.data.cooperationlist.concat(response.data.data.sh_items),
            page: that.data.page + 1,
            hasMore: true,
            listflag: true
          })
          wx.stopPullDownRefresh()
        } else {
          that.setData({
            hasMore: false,
            listflag: false
          })
          wx.stopPullDownRefresh()
        }

      }, function(error) {
        console.log(error);
      });
  },
  jumpadvert: function(e) { //广告位跳转
    app.AdSkipper(e)
  },
  viewgoodsDetail: function(e) {
    var that = this
    var pageurl = e.currentTarget.dataset.url
    var codeimg = e.currentTarget.dataset.codeimg
    this.setData({
      cardqrcode: '',
    })
    if (pageurl) {
      wx.showModal({
        title: '提示',
        content: '请复制链接，打开您的手机浏览器粘贴前往这个页面哦~',
        confirmText: "复制链接",
        confirmColor: "#c40000",
        showCancel: false,
        success: function(res) {
          if (res.confirm) {
            wx.setClipboardData({
              data: String(pageurl),
              success: function(res) {
                wx.getClipboardData({
                  success: function(res) {
                    if (res.data) {
                      // shihan.showToast.call(that, '复制成功');
                    }
                  }
                })
              }
            })
          }
        }
      })
    } else {
      this.animation.translateY(-range).step();
      this.setData({
        filterConflag: true,
        cardqrcode: codeimg,
        animationData: this.animation.export()
      })
    }
  },
  saveImgToPhotosAlbumTap: function() {
    wx.downloadFile({
      url: this.data.cardqrcode,
      success: function(res) {
        shihan.saveImage(res.tempFilePath); //保存图片
      }
    })

  },
  closeconcat: function() {
    this.animation.translateY(range).step();
    this.setData({
      filterConflag: false,
      animationData: this.animation.export()
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.setData({
      page: 1,
      cooperationlist: [],
      showLoading: true
    });
    if (this.data.cooperationlist == 0) {
      this.changecooperationlist()
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    if (this.data.listflag) {
      this.setData({
        listflag: false
      })
      this.changecooperationlist()
    }
  }
})